﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.MEP;

namespace YArchitech.Plumbing
{
	internal class ClassHideOrShowOpera
	{
		public ClassHideOrShowOpera()
		{
		}

		public ClassHideOrShowOpera(UIDocument uiDocument, Document document)
		{
			this.uiDoc = uiDocument;
			this.doc = document;
			this.hideShow = new HideShowElement(document, uiDocument);
			this.afo = new AuxiliaryFrameOperation();
		}

		public void HideOprea(List<SelectElems> selectElems)
		{
			using (List<SelectElems>.Enumerator enumerator = selectElems.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					switch (enumerator.Current)
					{
					case SelectElems.SpinkLer:
						this.hideShow.spinkLerHide();
						break;
					case SelectElems.AuxiliaryFrame:
						this.afo.DeleteAllAuxiliaryFrame(this.doc);
						break;
					case SelectElems.SprayRadius:
						this.listElems = this.GetSprayByActive(this.doc, this.doc.ActiveView);
						this.ClearElemPR(this.doc, this.listElems);
						break;
					case SelectElems.FireHydrantRadius:
						this.listElems = this.GetFireHydrantsByActive(this.doc, this.doc.ActiveView);
						this.ClearElemPR(this.doc, this.listElems);
						break;
					}
					this.listElems.Clear();
				}
			}
		}

		public void ShowOprea(List<SelectElems> selectElems)
		{
			using (List<SelectElems>.Enumerator enumerator = selectElems.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					switch (enumerator.Current)
					{
					case SelectElems.SpinkLer:
						this.hideShow.showLerHide();
						break;
					case SelectElems.AuxiliaryFrame:
						this.afo.ShowAllAuxiliaryFrame(this.uiDoc);
						break;
					case SelectElems.SprayRadius:
						this.listElems = this.GetSprayByActive(this.doc, this.doc.ActiveView);
						this.createPR.CreatActiveViewDetailCurve(this.doc, this.listElems, this.hyGPS);
						break;
					case SelectElems.FireHydrantRadius:
						this.listElems = this.GetFireHydrantsByActive(this.doc, this.doc.ActiveView);
						this.createPR.CreatActiveViewDetailCurve(this.doc, this.listElems, this.hyGPS);
						break;
					}
					this.listElems.Clear();
				}
			}
		}

		private List<Element> GetSprayByActive(Document doc, Autodesk.Revit.DB.View activeView)
		{
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_Sprinklers);
			return new FilteredElementCollector(doc, activeView.Id).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements().ToList<Element>();
		}

		private List<Element> GetFireHydrantsByActive(Document doc, Autodesk.Revit.DB.View activeView)
		{
			ElementCategoryFilter elementCategoryFilter = new ElementCategoryFilter(BuiltInCategory.OST_MechanicalEquipment);
			return new FilteredElementCollector(doc, activeView.Id).WherePasses(elementCategoryFilter).WhereElementIsNotElementType().ToElements().ToList<Element>();
		}

		public void ClearElemPR(Document doc, IList<Element> listElems)
		{
			foreach (Element element in listElems)
			{
				this.clearPR.DeleteTags(doc, element);
			}
		}

		private UIDocument uiDoc;

		private Document doc;

		private HideShowElement hideShow;

		private AuxiliaryFrameOperation afo;

		private CreateProtectRange createPR = new CreateProtectRange();

		private IList<Element> listElems = new List<Element>();

		private ClearProtectRange clearPR = new ClearProtectRange();

		private HyRangeCheckMajor hyGPS;
	}
}
